package gui.clientes;

import bin.clientes.Cliente;
import bin.clientes.Telefone;
import gui.JanelaPrincipal;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;

public class JanelaPesquisaClientes extends javax.swing.JInternalFrame {

    //Atributos
    private JanelaPrincipal janelaPrincipal;
    private JTextComponent[] camposFiltros;
    
    //Flags
    private boolean pesquisaPersonalizada;
    
    //Construtor
    public JanelaPesquisaClientes(JanelaPrincipal janelaPrincipal) {
        this.janelaPrincipal = janelaPrincipal;
        
        initComponents();
        
        this.pesquisaPersonalizada = false;
        this.camposFiltros = new JTextComponent[]{this.txtPesqNome, this.txtPesqCidade, this.txtPesqCpf, this.txtPesqCnpj, this.txtPesqEstado};
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GrupoBotoes = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbClientes = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtCidade = new javax.swing.JTextField();
        txtPais = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbTelefone = new javax.swing.JTable();
        txtRg = new javax.swing.JTextField();
        lbIconClientes = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        txtTipo = new javax.swing.JTextField();
        txtCnpj = new javax.swing.JTextField();
        txtCpf = new javax.swing.JTextField();
        txtEstado = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        txtPesqNome = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtPesqCidade = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtPesqEstado = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtPesqCpf = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        txtPesqCnpj = new javax.swing.JFormattedTextField();
        cbPesqFisico = new javax.swing.JCheckBox();
        cbPesqJuridico = new javax.swing.JCheckBox();
        jLabel16 = new javax.swing.JLabel();
        rbTodos = new javax.swing.JRadioButton();
        rbPersonalizada = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JSeparator();
        btAtualizar = new javax.swing.JButton();
        btC1 = new javax.swing.JButton();
        btC2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();

        setTitle("Consulta de Clientes");

        tbClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Nome"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbClientes.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tbClientes.setShowHorizontalLines(false);
        tbClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbClientesMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tbClientes);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel2.setText("Nome:");

        jLabel3.setText("Endereço:");

        jLabel4.setText("Cpf:");

        jLabel1.setText("Cidade:");

        jLabel6.setText("Estado:");

        jLabel7.setText("País:");

        jLabel8.setText("Cnpj:");

        jLabel9.setText("Rg:");

        txtEndereco.setEditable(false);

        txtNome.setEditable(false);

        txtCidade.setEditable(false);

        txtPais.setEditable(false);

        tbTelefone.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo", "Telefone"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbTelefone.setShowHorizontalLines(false);
        jScrollPane2.setViewportView(tbTelefone);

        txtRg.setEditable(false);

        lbIconClientes.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lbIconClientes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/icons/cadastroCliente.png"))); // NOI18N

        jLabel5.setText("Telefones:");

        jLabel17.setText("Tipo:");

        txtTipo.setEditable(false);

        txtCnpj.setEditable(false);

        txtCpf.setEditable(false);

        txtEstado.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel8))
                                .addGap(28, 28, 28)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNome)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(txtCnpj)
                                        .addGap(164, 164, 164)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel17))
                                .addGap(22, 22, 22)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtTipo)
                                    .addComponent(txtCidade))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtPais, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtRg, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtEndereco))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 436, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                                .addComponent(lbIconClientes)))
                        .addGap(10, 10, 10))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtPais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(txtRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel17))
                    .addComponent(txtTipo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel4)
                            .addComponent(txtCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lbIconClientes))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Filtros"));

        jLabel10.setText("Nome:");

        txtPesqNome.setEditable(false);

        jLabel11.setText("Cidade:");

        txtPesqCidade.setEditable(false);

        jLabel12.setText("Estado:");

        txtPesqEstado.setEditable(false);

        jLabel13.setText("Cpf:");

        txtPesqCpf.setEditable(false);
        try {
            txtPesqCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel14.setText("Cnpj:");

        txtPesqCnpj.setEditable(false);
        try {
            txtPesqCnpj.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        cbPesqFisico.setSelected(true);
        cbPesqFisico.setText("Fisico");
        cbPesqFisico.setEnabled(false);

        cbPesqJuridico.setSelected(true);
        cbPesqJuridico.setText("Jurídico");
        cbPesqJuridico.setEnabled(false);

        jLabel16.setText("Pesquisa:");

        GrupoBotoes.add(rbTodos);
        rbTodos.setSelected(true);
        rbTodos.setText("Todos");
        rbTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTodosActionPerformed(evt);
            }
        });

        GrupoBotoes.add(rbPersonalizada);
        rbPersonalizada.setText("Personalizada");
        rbPersonalizada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbPersonalizadaActionPerformed(evt);
            }
        });

        btAtualizar.setText("Atualizar Tabela");
        btAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAtualizarActionPerformed(evt);
            }
        });

        btC1.setText("C");
        btC1.setEnabled(false);
        btC1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btC1ActionPerformed(evt);
            }
        });

        btC2.setText("C");
        btC2.setEnabled(false);
        btC2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btC2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPesqNome, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtPesqCnpj, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                                    .addComponent(txtPesqCpf))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btC1)
                                    .addComponent(btC2))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(cbPesqFisico, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbPesqJuridico))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtPesqEstado, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                                    .addComponent(txtPesqCidade)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btAtualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel16)
                        .addGap(18, 18, 18)
                        .addComponent(rbTodos)
                        .addGap(18, 18, 18)
                        .addComponent(rbPersonalizada))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(rbTodos)
                    .addComponent(rbPersonalizada)
                    .addComponent(btAtualizar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtPesqNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(txtPesqCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtPesqEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(txtPesqCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btC1))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(txtPesqCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbPesqJuridico)
                    .addComponent(cbPesqFisico)
                    .addComponent(btC2))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jMenu1.setText("Fechar");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Tipo de pesquisa Padrão ou Personalizada (Ativar ou desativar campos)
    private void rbTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTodosActionPerformed
        if(this.rbTodos.isSelected())
        {
            liberarComponetesPesquisa(false);
            return;
        }
        liberarComponetesPesquisa(true);
    }//GEN-LAST:event_rbTodosActionPerformed
    private void rbPersonalizadaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbPersonalizadaActionPerformed
        if(this.rbPersonalizada.isSelected())
        {
            liberarComponetesPesquisa(true);
            return;
        }
        liberarComponetesPesquisa(false);
    }//GEN-LAST:event_rbPersonalizadaActionPerformed

    //Liberar ou bloquear os componentes de pesquisa
    public void liberarComponetesPesquisa(boolean liberar)
    {
        for(int i = 0; i < this.camposFiltros.length; i++)
        {
            this.camposFiltros[i].setEditable(liberar);
        }
        this.cbPesqFisico.setEnabled(liberar);
        this.btC1.setEnabled(liberar);
        this.btC2.setEnabled(liberar);
        this.cbPesqJuridico.setEnabled(liberar);
        this.pesquisaPersonalizada = liberar;
    }
    
    //Botão para atualizar tabela
    private void btAtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAtualizarActionPerformed
        this.atualizarTabela();
    }//GEN-LAST:event_btAtualizarActionPerformed

    //Botão para apagar o campo de CPF
    private void btC1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btC1ActionPerformed
        this.txtPesqCpf.setText("");
    }//GEN-LAST:event_btC1ActionPerformed
    //Botão para apagar o campo de CNPJ
    private void btC2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btC2ActionPerformed
        this.txtPesqCnpj.setText("");
    }//GEN-LAST:event_btC2ActionPerformed

    //Fechar Janela
    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseClicked
        this.janelaPrincipal.getJanelaFactory().liberarJanela("[Menu, Pesquisa, Cliente]");
        this.dispose();
    }//GEN-LAST:event_jMenu1MouseClicked

    //Ao soltar o mouse mostrar informações do cliente
    private void tbClientesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbClientesMouseReleased
        this.selecionarCliente();
    }//GEN-LAST:event_tbClientesMouseReleased

    //Seleiconar Cliente
    public void selecionarCliente()
    {
        this.limparCampos();
        if(this.tbClientes.getSelectedRow() == -1)
        {
            return;
        }
        for(Cliente c : this.janelaPrincipal.getArmazem().getListaClientes())
        {
            if(c.getId() == (int)this.tbClientes.getValueAt(this.tbClientes.getSelectedRow(), 0))
            {
                this.mostrarInformacoesCliente(c);
                return;
            }
        }
    }
    
    //Limpar campos de informação do cliente
    public void limparCampos()
    {
        this.txtNome.setText("");
        this.txtEndereco.setText("");
        this.txtTipo.setText("");
        this.txtCpf.setText("");
        this.txtCnpj.setText("");
        this.txtCidade.setText("");
        this.txtEstado.setText("");
        this.txtPais.setText("");
        this.txtRg.setText("");
        
        DefaultTableModel dtm = (DefaultTableModel)this.tbTelefone.getModel();
        while(dtm.getRowCount() > 0)
        {
            dtm.removeRow(0);
        }
    }
    
    //Atualizar tanela referente as pesquisas
    public void atualizarTabela()
    {
        this.limparTabela();
        if(!this.pesquisaPersonalizada)
        {
            this.pesquisaPadrao();
        }
        else
        {
            this.pesquisaPersonalizada();
        }
    }
    
    //Limpar tabela
    public void limparTabela()
    {
        DefaultTableModel dtm = (DefaultTableModel)this.tbClientes.getModel();
        while(dtm.getRowCount() > 0)
        {
            dtm.removeRow(0);
        }
    }
    
    //Pesquisa todos os clientes normalmente
    private void pesquisaPadrao()
    {
        DefaultTableModel dtm = (DefaultTableModel)this.tbClientes.getModel();
        for(Cliente c : this.janelaPrincipal.getArmazem().getListaClientes())
        {
            dtm.addRow(new Object[]{c.getId(), c.getNome()});
        }
    }
    
    //Pesquisa personalizada. Pegará os valores dos campos para trazer somente os clientes respectivos com estes valores
    private void pesquisaPersonalizada()
    {
        //Campos que servirao como filtro
        String[] valoresFiltro = new String[this.camposFiltros.length];
        
        for(int i = 0; i < this.camposFiltros.length; i++)
        {
            if(!this.camposFiltros[i].getText().equals(""))
            {
                valoresFiltro[i] = this.camposFiltros[i].getText();
            }
        }
        
        DefaultTableModel dtm = (DefaultTableModel)this.tbClientes.getModel();
        for(Cliente c : this.janelaPrincipal.getArmazem().getListaClientes())
        {
            if(!c.getNome().equals(valoresFiltro[0]) && valoresFiltro[0] != null)
            {
                continue;
            }
            if(!c.getCidade().equals(valoresFiltro[1]) && valoresFiltro[1] != null)
            {
                continue;
            }
            if(!c.getCpf().equals(valoresFiltro[2]) && !valoresFiltro[2].equals("   .   .   -  "))
            {
                continue;
            }
            if(!c.getCnpj().equals(valoresFiltro[3])  && !valoresFiltro[3].equals("  .   .   /    -  "))
            {
                continue;
            }
            if(!c.getEstado().equals(valoresFiltro[4]) && valoresFiltro[4] != null)
            {
                continue;
            }
            if(c.isFisico() && !this.cbPesqFisico.isSelected())
            {
                continue;
            }
            if(!c.isFisico() && !this.cbPesqJuridico.isSelected())
            {
                continue;
            }
            
            dtm.addRow(new Object[]{c.getId(), c.getNome()});
        }
    }
    
    //Mostrar informações do cliente selecionado
    public void mostrarInformacoesCliente(Cliente c)
    {
        this.txtNome.setText(c.getNome());
        this.txtEndereco.setText(c.getEndereco());
        this.txtTipo.setText(c.isFisico() ? "Físico" : "Jurídico");
        this.txtCpf.setText(c.getCpf());
        this.txtCnpj.setText(c.getCnpj());
        this.txtCidade.setText(c.getCidade());
        this.txtEstado.setText(c.getEstado());
        this.txtPais.setText(c.getPais());
        this.txtRg.setText(c.getRg());
        
        DefaultTableModel dtm = (DefaultTableModel)this.tbTelefone.getModel();
        for(Telefone t : c.getListaTelefones())
        {
            dtm.addRow(new Object[]{t.getTipo(), t.getTelefone()});
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup GrupoBotoes;
    private javax.swing.JButton btAtualizar;
    private javax.swing.JButton btC1;
    private javax.swing.JButton btC2;
    private javax.swing.JCheckBox cbPesqFisico;
    private javax.swing.JCheckBox cbPesqJuridico;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbIconClientes;
    private javax.swing.JRadioButton rbPersonalizada;
    private javax.swing.JRadioButton rbTodos;
    private javax.swing.JTable tbClientes;
    private javax.swing.JTable tbTelefone;
    private javax.swing.JTextField txtCidade;
    private javax.swing.JTextField txtCnpj;
    private javax.swing.JTextField txtCpf;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtEstado;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtPais;
    private javax.swing.JTextField txtPesqCidade;
    private javax.swing.JFormattedTextField txtPesqCnpj;
    private javax.swing.JFormattedTextField txtPesqCpf;
    private javax.swing.JTextField txtPesqEstado;
    private javax.swing.JTextField txtPesqNome;
    private javax.swing.JTextField txtRg;
    private javax.swing.JTextField txtTipo;
    // End of variables declaration//GEN-END:variables
}
